import re
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import re
import xml.etree.ElementTree as ET
def parse_path(tokens):
    """
    解析路径字符串d，返回路径列表
    """

    path = []
    last_point = (0, 0)  # 添加一个起始点
    # print("aaa",tokens)
    for token in tokens:
        command = token[0]
        # print("BBB",token)
        param =list(token)
        # print(param)
        params=param[1].strip().split()
        print(params)
        if command == 'M':
            start = (float(params[0]), float(params[1]))
            path.append(start)
            last_point = start
        elif command == 'L':
            end = (float(params[0]), float(params[1]))
            path.append(end)
            last_point = end
        elif command == 'H':
            end = (float(params[0]), last_point[1])
            path.append(end)
            last_point = end
        elif command == 'V':
            end = (last_point[0], float(params[0]))
            path.append(end)
            last_point = end
        elif command == 'Q':
            control = (float(params[0]), float(params[1]))
            end = (float(params[2]), float(params[3]))
            path.append(control)
            path.append(end)
            last_point = end
        # 添加其他命令的处理逻辑
        elif command == 'T':
            end = (float(params[0]), float(params[1]))
            path.append(end)
            last_point = end
        elif command == 'S':
            control = (float(params[0]), float(params[1]))
            end = (float(params[2]), float(params[3]))
            path.append(control)
            path.append(end)
            last_point = end
        elif command == 'C':
            control1 = (float(params[0]), float(params[1]))
            control2 = (float(params[2]), float(params[3]))
            end = (float(params[4]), float(params[5]))
            path.append(control1)
            path.append(control2)
            path.append(end)
            last_point = end
        elif command == 'A':
            end = (float(params[5]), float(params[6]))  # A命令有7个参数
            path.append(end)
            last_point = end
        elif command == 'z':
            end = start
            path.append(end)
            last_point = end

    return path

def add_edges_from_path(G, path):
    """
    根据路径path，向图G中添加边
    """
    for i in range(len(path)-1):
        G.add_edge(path[i], path[i+1])





# 读取SVG文件
tree = ET.parse('map.svg')
root = tree.getroot()
with open("map.svg", "r", encoding="utf-8") as file:
    data = file.read()
# print(data)
# paths=data.get('d')
paths = root.findall('.//{http://www.w3.org/2000/svg}path')
print(paths)
G = nx.DiGraph()
print(len(paths)-1)
for path in paths:
    path_data=path.get('d')
    # print(path_data)
    if path_data==None:
        continue
    pattern = r'([MLHVCSQTAz])([^MLHVCSQTAz]+)'
    new_path = re.findall(pattern, path_data)

    # print("CCC",path)
    # print(type(path))
    # print(type(path))

    new_path=parse_path(new_path)
    # print("DDD",'aaaaaa')
    add_edges_from_path(G, new_path)
    print("----------------------------------------------------------------")

for path in paths:
    path=path.get("d")
    print(path)
print(len(paths))
# print(G.edges)
# print(G.nodes)
print(G)
# print(G)
pos = nx.spring_layout(G)
print("aaaa")

nx.draw(G, pos,node_size=2)
plt.show()
